package main

import (
	"oa-rest/common"
	"oa-rest/config"
	"oa-rest/models/task"
	"xorm.io/xorm"

	log "github.com/sirupsen/logrus"
)

func main() {
	config.InitEnv()
	db, err := common.ConnectDb()
	if err != nil {
		println(err)
	}
	defer db.Close()
	_, err = db.Exec("drop table if exists task")
	_, err = db.Exec("drop table if exists task_node")
	_, err = db.Exec("drop table if exists task_node_item")
	_, err = db.Exec("drop table if exists task_attachments")
	_, err = db.Exec("drop table if exists task_template")
	_, err = db.Exec("drop table if exists task_template_node")
	_, err = db.Exec("drop table if exists task_template_node_item")
	_, err = db.Exec("drop table if exists task_comment")
	if err != nil {
		log.Fatal(err)
	}

	err = db.Sync2(
		new(task.Task),
		new(task.TaskNode),
		new(task.TaskNodeItem),
		new(task.TaskTemplate),
		new(task.TaskTemplateNode),
		new(task.TaskTemplateNodeItem),
		new(task.TaskAttachments),
		new(task.TaskComment),
	)
	if err != nil {
		log.Fatal(err)
	}

	initTemplate(db)
	initTemplateNode(db)
}

func initTemplate(db *xorm.Engine) {
	_, err := db.Insert([]task.TaskTemplate{
		{Type: 1, Mode: 1, Name: "通知", Upto: "下行文", Remark: "部署工作、印发文件、告知事项"},
		{Type: 2, Mode: 1, Name: "通报", Upto: "下行文", Remark: "表彰先进、批评错误、沟通情况"},
		{Type: 3, Mode: 1, Name: "函", Upto: "平行文", Remark: "与不相隶属单位商洽工作、询问答复"},
		{Type: 4, Mode: 1, Name: "报告", Upto: "上行文", Remark: "向上级单位汇报工作、反映情况"},
		{Type: 5, Mode: 1, Name: "请示", Upto: "上行文", Remark: "向上级单位请求指示和批准"},
		{Type: 6, Mode: 1, Name: "纪要", Upto: "内部/下行文", Remark: "记录和传达会议情况与决议"},
		{Type: 7, Mode: 1, Name: "决定", Upto: "下行文", Remark: "对重要事项作出决策或进行奖惩"},
		{Type: 8, Mode: 1, Name: "意见", Upto: "多向行文", Remark: "对重要问题提出见解和处理办法"},
		{Type: 101, Mode: 2, Name: "提案", Upto: "自定义", Remark: "自定义提案"},
	})
	if err != nil {
		log.Fatal(err)
	}
}

func initTemplateNode(db *xorm.Engine) {
	// 1. 通知类公文审批流程
	_, err := db.Insert([]task.TaskTemplateNode{
		{TemplateId: 1, Name: "部门主任审核", Pid: 0},   // 第一级，pid为0
		{TemplateId: 1, Name: "秘书长审批", Pid: 1},     // 第二级，pid为第一级的id
		{TemplateId: 1, Name: "专职副会长签发", Pid: 2}, // 第三级，pid为第二级的id
	})

	_, err = db.Insert([]task.TaskTemplateNodeItem{
		// 部门主任审核节点(节点id=1)的审批人
		{NodeId: 1, Username: "010", RealName: "孙士磊"}, // 培训部
		{NodeId: 1, Username: "012", RealName: "相彬"},   // 法律工作部
		{NodeId: 1, Username: "016", RealName: "徐晓辉"}, // 财险工作部
		{NodeId: 1, Username: "020", RealName: "袁庆霞"}, // 寿险工作部
		{NodeId: 1, Username: "023", RealName: "朱睿"},   // 银行工作部

		// 秘书长审批节点(节点id=2)的审批人
		{NodeId: 2, Username: "002", RealName: "郑浩"},

		// 专职副会长签发节点(节点id=3)的审批人
		{NodeId: 3, Username: "001", RealName: "张夫贵"},
	})

	// 2. 通报类公文审批流程
	_, err = db.Insert([]task.TaskTemplateNode{
		{TemplateId: 2, Name: "相关部门主任审核", Pid: 0},
		{TemplateId: 2, Name: "分管副秘书长审批", Pid: 4},
		{TemplateId: 2, Name: "秘书长签发", Pid: 5},
	})

	_, err = db.Insert([]task.TaskTemplateNodeItem{
		// 相关部门主任审核节点(节点id=4)的审批人
		{NodeId: 4, Username: "010", RealName: "孙士磊"},
		{NodeId: 4, Username: "012", RealName: "相彬"},
		{NodeId: 4, Username: "016", RealName: "徐晓辉"},
		{NodeId: 4, Username: "020", RealName: "袁庆霞"},
		{NodeId: 4, Username: "023", RealName: "朱睿"},

		// 分管副秘书长审批节点(节点id=5)的审批人
		{NodeId: 5, Username: "003", RealName: "邢业久"},
		{NodeId: 5, Username: "004", RealName: "任海军"},
		{NodeId: 5, Username: "005", RealName: "董晓燕"},

		// 秘书长签发节点(节点id=6)的审批人
		{NodeId: 6, Username: "002", RealName: "郑浩"},
	})

	// 3. 函类公文审批流程
	_, err = db.Insert([]task.TaskTemplateNode{
		{TemplateId: 3, Name: "相关部门主任审核", Pid: 0},
		{TemplateId: 3, Name: "分管副秘书长审批", Pid: 7},
	})

	_, err = db.Insert([]task.TaskTemplateNodeItem{
		// 相关部门主任审核节点(节点id=7)的审批人
		{NodeId: 7, Username: "010", RealName: "孙士磊"},
		{NodeId: 7, Username: "012", RealName: "相彬"},
		{NodeId: 7, Username: "016", RealName: "徐晓辉"},
		{NodeId: 7, Username: "020", RealName: "袁庆霞"},
		{NodeId: 7, Username: "023", RealName: "朱睿"},

		// 分管副秘书长审批节点(节点id=8)的审批人
		{NodeId: 8, Username: "003", RealName: "邢业久"},
		{NodeId: 8, Username: "004", RealName: "任海军"},
		{NodeId: 8, Username: "005", RealName: "董晓燕"},
	})

	// 4. 报告类公文审批流程
	_, err = db.Insert([]task.TaskTemplateNode{
		{TemplateId: 4, Name: "部门主任审核", Pid: 0},
		{TemplateId: 4, Name: "分管副秘书长审批", Pid: 9},
		{TemplateId: 4, Name: "秘书长审签", Pid: 10},
	})

	_, err = db.Insert([]task.TaskTemplateNodeItem{
		// 部门主任审核节点(节点id=9)的审批人
		{NodeId: 9, Username: "010", RealName: "孙士磊"},
		{NodeId: 9, Username: "012", RealName: "相彬"},
		{NodeId: 9, Username: "016", RealName: "徐晓辉"},
		{NodeId: 9, Username: "020", RealName: "袁庆霞"},
		{NodeId: 9, Username: "023", RealName: "朱睿"},

		// 分管副秘书长审批节点(节点id=10)的审批人
		{NodeId: 10, Username: "003", RealName: "邢业久"},
		{NodeId: 10, Username: "004", RealName: "任海军"},
		{NodeId: 10, Username: "005", RealName: "董晓燕"},

		// 秘书长审签节点(节点id=11)的审批人
		{NodeId: 11, Username: "002", RealName: "郑浩"},
	})

	// 5. 请示类公文审批流程
	_, err = db.Insert([]task.TaskTemplateNode{
		{TemplateId: 5, Name: "部门主任审核", Pid: 0},
		{TemplateId: 5, Name: "分管副秘书长审核", Pid: 12},
		{TemplateId: 5, Name: "秘书长审核", Pid: 13},
		{TemplateId: 5, Name: "专职副会长审批", Pid: 14},
	})

	_, err = db.Insert([]task.TaskTemplateNodeItem{
		// 部门主任审核节点(节点id=12)的审批人
		{NodeId: 12, Username: "010", RealName: "孙士磊"},
		{NodeId: 12, Username: "012", RealName: "相彬"},
		{NodeId: 12, Username: "016", RealName: "徐晓辉"},
		{NodeId: 12, Username: "020", RealName: "袁庆霞"},
		{NodeId: 12, Username: "023", RealName: "朱睿"},

		// 分管副秘书长审核节点(节点id=13)的审批人
		{NodeId: 13, Username: "003", RealName: "邢业久"},
		{NodeId: 13, Username: "004", RealName: "任海军"},
		{NodeId: 13, Username: "005", RealName: "董晓燕"},

		// 秘书长审核节点(节点id=14)的审批人
		{NodeId: 14, Username: "002", RealName: "郑浩"},

		// 专职副会长审批节点(节点id=15)的审批人
		{NodeId: 15, Username: "001", RealName: "张夫贵"},
	})

	// 6. 纪要类公文审批流程
	_, err = db.Insert([]task.TaskTemplateNode{
		{TemplateId: 6, Name: "记录人整理", Pid: 0},
		{TemplateId: 6, Name: "会议主持人审核", Pid: 16},
		{TemplateId: 6, Name: "秘书长签发", Pid: 17},
	})

	_, err = db.Insert([]task.TaskTemplateNodeItem{
		// 记录人整理节点(节点id=16)的审批人
		{NodeId: 16, Username: "006", RealName: "苏丹"},
		{NodeId: 16, Username: "007", RealName: "孙鹏然"},
		{NodeId: 16, Username: "008", RealName: "李叶子"},

		// 会议主持人审核节点(节点id=17)的审批人
		{NodeId: 17, Username: "001", RealName: "张夫贵"},
		{NodeId: 17, Username: "002", RealName: "郑浩"},
		{NodeId: 17, Username: "003", RealName: "邢业久"},
		{NodeId: 17, Username: "004", RealName: "任海军"},
		{NodeId: 17, Username: "005", RealName: "董晓燕"},

		// 秘书长签发节点(节点id=18)的审批人
		{NodeId: 18, Username: "002", RealName: "郑浩"},
	})

	// 7. 决定类公文审批流程
	_, err = db.Insert([]task.TaskTemplateNode{
		{TemplateId: 7, Name: "相关部门主任审核", Pid: 0},
		{TemplateId: 7, Name: "分管副秘书长审核", Pid: 19},
		{TemplateId: 7, Name: "秘书长审核", Pid: 20},
		{TemplateId: 7, Name: "专职副会长签发", Pid: 21},
	})

	_, err = db.Insert([]task.TaskTemplateNodeItem{
		// 相关部门主任审核节点(节点id=19)的审批人
		{NodeId: 19, Username: "010", RealName: "孙士磊"},
		{NodeId: 19, Username: "012", RealName: "相彬"},
		{NodeId: 19, Username: "016", RealName: "徐晓辉"},
		{NodeId: 19, Username: "020", RealName: "袁庆霞"},
		{NodeId: 19, Username: "023", RealName: "朱睿"},

		// 分管副秘书长审核节点(节点id=20)的审批人
		{NodeId: 20, Username: "003", RealName: "邢业久"},
		{NodeId: 20, Username: "004", RealName: "任海军"},
		{NodeId: 20, Username: "005", RealName: "董晓燕"},

		// 秘书长审核节点(节点id=21)的审批人
		{NodeId: 21, Username: "002", RealName: "郑浩"},

		// 专职副会长签发节点(节点id=22)的审批人
		{NodeId: 22, Username: "001", RealName: "张夫贵"},
	})

	// 8. 意见类公文审批流程
	_, err = db.Insert([]task.TaskTemplateNode{
		{TemplateId: 8, Name: "部门主任审核", Pid: 0},
		{TemplateId: 8, Name: "分管副秘书长审核", Pid: 23},
		{TemplateId: 8, Name: "秘书长审核", Pid: 24},
		{TemplateId: 8, Name: "专职副会长签发", Pid: 25},
	})

	_, err = db.Insert([]task.TaskTemplateNodeItem{
		// 部门主任审核节点(节点id=23)的审批人
		{NodeId: 23, Username: "010", RealName: "孙士磊"},
		{NodeId: 23, Username: "012", RealName: "相彬"},
		{NodeId: 23, Username: "016", RealName: "徐晓辉"},
		{NodeId: 23, Username: "020", RealName: "袁庆霞"},
		{NodeId: 23, Username: "023", RealName: "朱睿"},

		// 分管副秘书长审核节点(节点id=24)的审批人
		{NodeId: 24, Username: "003", RealName: "邢业久"},
		{NodeId: 24, Username: "004", RealName: "任海军"},
		{NodeId: 24, Username: "005", RealName: "董晓燕"},

		// 秘书长审核节点(节点id=25)的审批人
		{NodeId: 25, Username: "002", RealName: "郑浩"},

		// 专职副会长签发节点(节点id=26)的审批人
		{NodeId: 26, Username: "001", RealName: "张夫贵"},
	})
	if err != nil {
		log.Fatal(err)
	}
}
